package com.lancoo.examview.exam;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.AnswerSheetChildBean;
import com.lancoo.answer.model.eventBean.AnswerSheetEventBean;
import com.lancoo.answer.util.paperUtils.KindIndexStringUtils;
import com.lancoo.answer.widget.ChildFlowLayout;
import com.lancoo.examview.R;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by pengmi on 2022/2/23 15:14
 * Description:
 */
public class QueseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Type> typeList;
    private int startTypeIndex, startQuesIndex;
    private int endTypeIndex,endQuesIndex;
    private OnLimitIndexCallBack callCallBack;

    public QueseAdapter(List<Type> typeList) {
        this.typeList = typeList;
    }

    public void setCallCallBack(OnLimitIndexCallBack callCallBack) {
        this.callCallBack = callCallBack;
    }

    public void setLimitIndex(int startTypeIndex, int startQuesIndex, int endTypeIndex, int endQuesIndex){
        this.startTypeIndex=startTypeIndex;
        this.startQuesIndex=startQuesIndex;
        this.endTypeIndex=endTypeIndex;
        this.endQuesIndex=endQuesIndex;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.ev_item_type_answer_sheet, parent, false);
        return new VH(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh= (VH) holder;
        vh.initData(position,typeList.get(position));
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }


    private class  VH extends RecyclerView.ViewHolder{


        public VH(@NonNull View itemView) {
            super(itemView);
        }

        public void initData(final int position,final Type type){
            TextView tv_kindName=itemView.findViewById(com.lancoo.answer.R.id.tv_kind_name);
            int typeIndex = type.getTypeIndex();
            if(typeIndex!=0){
                typeIndex=typeIndex-1;
            }else{
                typeIndex=position;
            }
            String name= KindIndexStringUtils.getOrderIndex(typeIndex)+"、"+type.getGenreName();
            tv_kindName.setText(name);
            ChildFlowLayout childFlowLayout = itemView.findViewById(com.lancoo.answer.R.id.child_flow_layout);
            childFlowLayout.removeAllViews();
            int index=1;
            for (Ques ques:type.getQuesList()){
                childFlowLayout.addView(addView(ques,index,position));
                index++;
            }
            childFlowLayout.setOnItemClickListener((position1, view) -> {
                if(System.currentTimeMillis()- ConstantBean.Companion.getOnClickSubTime()<200){
                    return;
                }
                ConstantBean.Companion.setOnClickSubTime(System.currentTimeMillis());
                Log.e("点击","typeIndex:"+position);
                Log.e("点击","quesIndex:"+position1);
                if(callCallBack!=null){
                    callCallBack.onCallBack(position,position1);
                }
//                AnswerSheetChildBean bean = answerSheetBean.getAnswerSheetChildBeanList().get(position1);
//                AnswerSheetEventBean answerSheetEvent=new AnswerSheetEventBean();
//                answerSheetEvent.setTypeIndex(bean.getTypeIndex());
//                answerSheetEvent.setQuesIndex(bean.getQuesIndex());
//                answerSheetEvent.setChildIndex(bean.getChildIndex());
//                answerSheetEvent.setItemIndex(bean.getItemIndex());
//                EventBus.getDefault().post(answerSheetEvent);
            });
        }

        public View addView(Ques bean,int position,int typeIndex){
            LayoutInflater layoutInflater = LayoutInflater.from(itemView.getContext());
            @SuppressLint("InflateParams") View inflate = layoutInflater .inflate(com.lancoo.answer.R.layout.ev_item_child_answer,null);
            TextView textView=inflate.findViewById(com.lancoo.answer.R.id.tv_child_index);
            textView.setText(position+"");
            ImageView imageView=inflate.findViewById(com.lancoo.answer.R.id.img_select_flag);
            imageView.setVisibility(View.INVISIBLE);
            textView.setTextColor(0xff666666);
            textView.setBackgroundResource(com.lancoo.answer.R.mipmap.ev_icon_choice_white);
            imageView.setImageResource(com.lancoo.answer.R.drawable.ev_vector_up_arror_ccc);

            if(startTypeIndex==typeIndex&&startQuesIndex==position-1){
                textView.setTextColor(0xffffffff);
                textView.setBackgroundResource(com.lancoo.answer.R.mipmap.ev_icon_choice_blue);
            }

            if(endTypeIndex==typeIndex&&endQuesIndex==position-1){
                textView.setTextColor(0xffffffff);
                textView.setBackgroundResource(com.lancoo.answer.R.mipmap.ev_icon_choice_red);
            }
            return inflate;
        }
    }

    public static interface OnLimitIndexCallBack{

        void onCallBack(int typeIndex,int quesIndex);
    }


}
